package com.zj.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zj.common.Msg;
import com.zj.common.Result;
import com.zj.entity.*;
import com.zj.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @GetMapping
    public Result<List<UserInfo>> getUserByNumOrNameOrAdd(@RequestParam String queryKey){
        return Result.success(adminService.getUserByNumOrNameOrAdd(queryKey));
    }
    @GetMapping("/totalInfo")
    public Result getTotalInfo(){
        return Result.success(adminService.getTotalInfo());
    }
    @GetMapping("/selectGoods")
    public Result<IPage> selectGoodsByNameOrDescriptionOrUserName(@RequestParam String queryKey,@RequestParam int current,@RequestParam int size) {
        return Result.success(adminService.selectGoodsByNameOrDescriptionOrUserName(queryKey,current,size), Msg.SELECT_IS_SUCCESS);
    }
    @GetMapping("/selectUser")
    public Result<IPage> selectUserByNameOrNumOrAddress(@RequestParam String queryKey,@RequestParam int current,@RequestParam int size){
        return Result.success(adminService.selectUserByNameOrNumOrAddress(queryKey,current,size), Msg.SELECT_IS_SUCCESS);
    }
    @DeleteMapping("/deleteUserById")
    public Result deleteUserById(@RequestParam String ids){
        String[] id= ids.split(",");
        int row = adminService.deleteUserById(id);
        if (row>0){
            return Result.success(row,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_FAILURE);
        }
    }
    @DeleteMapping("/deleteGoodsById")
    public Result deleteGoodsById(@RequestParam String ids){
        String[] id= ids.split(",");
        int row = adminService.deleteGoodsById(id);
        if (row>0){
            return Result.success(row,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_FAILURE);
        }
    }
    @PutMapping("/updateGoodsById")
    public Result updateGoodsById(@RequestBody Map<Object,Object> map){
        Goods goods = JSON.parseObject(JSON.toJSONString(map.get("goods")),Goods.class);
        int row = adminService.updateGoodsById(goods);
        if (row > 0){
            return Result.success(row,Msg.UPDATE_IS_SUCCESS);
        }else if(row == -1){
            return Result.error(Msg.Goods_VISIBLE_ERROR);
        }else {
            return Result.error(Msg.UPDATE_IS_ERROR);
        }
    }
    @PutMapping("/updateUserById")
    public Result updateUserById(@RequestBody User user){
        if (adminService.updateUserById(user)>0){
            return Result.success(true,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_FAILURE);
        }
    }
    @PutMapping("/updateUserInfoById")
    public Result updateUserInfoById(@RequestBody UserInfo userInfo){
        if (adminService.updateUserInfoById(userInfo)>0){
            return Result.success(true,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_FAILURE);
        }
    }
    @PostMapping("/addUser")
    public Result addUserAdmin(@RequestBody User user){
        if (adminService.addUserAdmin(user)>0){
            return Result.success(true,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_FAILURE);
        }
    }
    @PostMapping("/addUserInfo")
    public Result addUserInfoAdmin(@RequestBody UserInfo userInfo){
        if (adminService.addUserInfoAdmin(userInfo)>0){
            return Result.success(true,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_FAILURE);
        }
    }
    @PostMapping("/addGoods")
    public Result addGoodsAdmin(@RequestBody Map<Object,Object> map){
        Goods goods = JSON.parseObject(JSON.toJSONString(map.get("goods")),Goods.class);
        int row = adminService.addGoodsAdmin(goods);
        if (row > 0){
            return Result.success(row,Msg.ADD_IS_SUCCESS);
        }else if(row == -1){
            return Result.error(Msg.USER_IS_NULL);
        }else {
            return Result.error(Msg.ADD_IS_ERROR);
        }
    }
    @GetMapping("/selectGoodsCategory")
    public Result<List<GoodsCategory>> getGoodsCategory(){
        if (adminService.getGoodsCategory().size()>0){
            return Result.success(adminService.getGoodsCategory(),Msg.SELECT_IS_SUCCESS);
        }else {
            return Result.error(Msg.SELECT_IS_ERROR);
        }
    }
    @PutMapping("/updateGoodsCategory")
    public  Result updateGoodsCategory(@RequestBody  Map<Object,Object> map){
        GoodsCategory goodsCategory = JSON.parseObject(JSON.toJSONString(map.get("goodsCategory")),GoodsCategory.class);
       int row = adminService.updateGoodsCategory(goodsCategory);
        if (row>0){
            return Result.success(row,Msg.UPDATE_IS_SUCCESS);
        }else {
            return Result.error(Msg.UPDATE_IS_ERROR);
        }
    }
    @PostMapping ("/addGoodsCategory")
    public Result  addGoodsCategoryByCid(@RequestBody Map<Object,Object> map){
        GoodsCategory goodsCategory = JSON.parseObject(JSON.toJSONString(map.get("goodsCategory")),GoodsCategory.class);
        int row =  adminService.addGoodsCategoryByCid(goodsCategory);
        if (row>0){
            return Result.success(row,Msg.ADD_IS_SUCCESS);
        }else {
            return Result.error(Msg.ADD_IS_ERROR);
        }
    }

    @DeleteMapping("/deleteGoodsCategory")
    public Result deleteGoodsCategory(@RequestParam int id){
        int row =  adminService.deleteGoodsCategoryById(id);
        if (row>0){
            return Result.success(row,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_SUCCESS);
        }
    }
    @PostMapping("/addGoodsCategoryOneLevel")
    public Result addGoodsCategoryOneLevel(@RequestBody Map<Object,Object> map){
        GoodsCategory goodsCategory = JSON.parseObject(JSON.toJSONString(map.get("goodsCategory")),GoodsCategory.class);
        int row =  adminService.addGoodsCategoryOneLevel(goodsCategory);
        if (row>0){
            return Result.success(row,Msg.ADD_IS_SUCCESS);
        }else {
            return Result.error(Msg.ADD_IS_ERROR);
        }
    }
    @GetMapping("/selectAddress")
    public Result<List<Address>> getAddress(){
        if (adminService.getAddress().size()>0){
            return Result.success(adminService.getAddress(),Msg.SELECT_IS_SUCCESS);
        }else {
            return Result.error(Msg.SELECT_IS_ERROR);
        }
    }
    @PutMapping("/updateAddress")
    public  Result updateAddress(@RequestBody  Map<Object,Object> map){
        Address address = JSON.parseObject(JSON.toJSONString(map.get("address")),Address.class);
        int row = adminService.updateAddress(address);
        if (row>0){
            return Result.success(row,Msg.UPDATE_IS_SUCCESS);
        }else {
            return Result.error(Msg.UPDATE_IS_ERROR);
        }
    }
    @PostMapping ("/addAddress")
    public Result  addAddressByCid(@RequestBody Map<Object,Object> map){
        Address address = JSON.parseObject(JSON.toJSONString(map.get("address")),Address.class);
        int row =  adminService.addAddressByCid(address);
        if (row>0){
            return Result.success(row,Msg.ADD_IS_SUCCESS);
        }else {
            return Result.error(Msg.ADD_IS_ERROR);
        }
    }

    @DeleteMapping("/deleteAddress")
    public Result deleteAddress(@RequestParam int id){
        int row =  adminService.deleteAddressById(id);
        if (row>0){
            return Result.success(row,Msg.DELETE_IS_SUCCESS);
        }else {
            return Result.error(Msg.DELETE_IS_FAILURE);
        }
    }
    @PostMapping("/addAddressOneLevel")
    public Result addAddressOneLevel(@RequestBody Map<Object,Object> map){
        Address address = JSON.parseObject(JSON.toJSONString(map.get("address")),Address.class);
        int row =  adminService.addAddressOneLevel(address);
        if (row>0){
            return Result.success(row,Msg.ADD_IS_SUCCESS);
        }else {
            return Result.error(Msg.ADD_IS_ERROR);
        }
    }




}
